package com.example.gkcircle;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.example.gkcircle.activity.BoundLoginActivity;
import com.example.gkcircle.activity.ComposeLostActivity;
import com.example.gkcircle.activity.ComposeLoveActivity;
import com.example.gkcircle.activity.LoginActivity;
import com.example.gkcircle.activity.LostAndFoundActivity;
import com.example.gkcircle.fragment.HomeFragment;
import com.example.gkcircle.fragment.LoveFragment;
import com.example.gkcircle.fragment.LibraryFragment;
import com.example.gkcircle.fragment.PersonalFragment;
import com.example.gkcircle.library.object.GKUser;
import com.example.gkcircle.utils.ActivityUtil;
import com.example.gkcircle.utils.FileUtil;
import com.example.gkcircle.utils.PicUtil;
import com.example.gkcircle.utils.ToastFactory;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;

/**
 * 管理主Tab bar的Activity
 * Created by Zhouhua on 15/12/18.
 */
public class MainActivity  extends BaseAppCompatActivity implements OnClickListener{

    private LoveFragment mTab02;
    private HomeFragment mTab01;
    private LibraryFragment mTab03;
    private PersonalFragment mTab04;

    //tabbar
    public static  RelativeLayout mTabbar;
    //底部四个按钮
    private LinearLayout mHome;
    public static LinearLayout mLove;
    private LinearLayout mLibrary;
    private LinearLayout mPersonal;
    private  TextView mHomeText;
    public static  TextView mLoveText;
    private TextView mLibraryText;
    private TextView mPersonalText;

    //浮动菜单
    public static int firstVisiableItem;
    public static FloatingActionMenu mFloatingActionMenu;
    public static FloatingActionButton mFloatingActionButton;
    public static SubActionButton mSubActionButtonMovie;
    public static SubActionButton mSubActionButtonLove;
    public static SubActionButton mSubActionButtonWish;

    private final int MOVIE_CODE=0;
    private final int LOVE_CODE=1;
    private final int WISH_CODE=2;

    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;


    @Override
    protected void initView() {
        setContentView(R.layout.fragment_main);
        mTabbar=(RelativeLayout)findViewById(R.id.tabbar);
        mHome = (LinearLayout) findViewById(R.id.tabbar_home);
        mLove = (LinearLayout) findViewById(R.id.tabbar_love);
        mLibrary = (LinearLayout) findViewById(R.id.tabbar_library);
        mPersonal = (LinearLayout) findViewById(R.id.tabbar_personal);
        mHomeText=(TextView)findViewById(R.id.tabbar_btn_home_text);
        mLoveText=(TextView)findViewById(R.id.tabbar_btn_love_text);
        mLibraryText=(TextView)findViewById(R.id.tabbar_btn_library_text);
        mPersonalText=(TextView)findViewById(R.id.tabbar_btn_personal_text);
    }

    @Override
    protected void initListener() {
        mHome.setOnClickListener(this);
        mLove.setOnClickListener(this);
        mLibrary.setOnClickListener(this);
        mPersonal.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        fragmentManager = this.getSupportFragmentManager();
        //设置默认第一页
        setTabSelection(0);
        //获取屏幕宽高
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        App.screenWidth=dm.widthPixels;
        App.screenHeight=dm.heightPixels;
        //创建文件目录
        FileUtil.CreateDir();

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.tabbar_home:
                setTabSelection(0);
                break;
            case R.id.tabbar_love:
                setTabSelection(1);
                break;
            case R.id.tabbar_library:
                setTabSelection(2);
                break;
            case R.id.tabbar_personal:
                setTabSelection(3);
                break;

            default:
                break;
        }
    }

    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     */
    private void setTabSelection(int index)
    {
        // 开启一个Fragment事务
        FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        if(MainActivity.mFloatingActionButton!=null){
            //关闭约吗的Menu
            MainActivity.mFloatingActionButton.setVisibility(View.GONE);
            if (MainActivity.mFloatingActionMenu.isOpen()) {
                MainActivity.mFloatingActionMenu.close(true);
            }
            MainActivity.mFloatingActionMenu.close(true);
        }
        switch (index)
        {
            case 0:
                // 重置按钮
                resetBtn();
                // 当点击了消息tab时，改变控件的图片和文字颜色
                ((ImageButton) mHome.findViewById(R.id.tabbar_btn_home))
                        .setImageResource(R.mipmap.tabbar_home_pressed);
                mHomeText.setTextColor(getResources().getColor(R.color.main));
                if (mTab01 == null)
                {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    mTab01 = new HomeFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(mTab01);
                }
                break;
            case 1:
                //为约吗创建menu
                setFloatingMenu();
                //显示约吗的Menu
                MainActivity.mFloatingActionButton.setVisibility(View.VISIBLE);
                // 重置按钮
                resetBtn();
                // 当点击了消息tab时，改变控件的图片和文字颜色
                ((ImageButton) mLove.findViewById(R.id.tabbar_btn_love))
                        .setImageResource(R.mipmap.tabbar_love_pressed);
                mLoveText.setTextColor(getResources().getColor(R.color.main));
                if (mTab02 == null)
                {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    mTab02 = new LoveFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else
                {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(mTab02);
                }
                break;
            case 2:

                // 重置按钮
                resetBtn();
                // 当点击了动态tab时，改变控件的图片和文字颜色
                ((ImageButton) mLibrary.findViewById(R.id.tabbar_btn_library))
                        .setImageResource(R.mipmap.tabbar_reading_pressed);
                mLibraryText.setTextColor(getResources().getColor(R.color.main));
                if (mTab03 == null)
                {
                    // 如果NewsFragment为空，则创建一个并添加到界面上
                    mTab03 = new LibraryFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    // 如果NewsFragment不为空，则直接将它显示出来
                    transaction.show(mTab03);
                }
                break;
            case 3:
                //判断是否已登录
                if(AVUser.getCurrentUser()==null){
                    ActivityUtil.startActivity(this, LoginActivity.class);
                    return;
                }
                // 重置按钮
                resetBtn();
                // 当点击了设置tab时，改变控件的图片和文字颜色
                ((ImageButton) mPersonal.findViewById(R.id.tabbar_btn_personal))
                        .setImageResource(R.mipmap.tabbar_user_pressed);
                mPersonalText.setTextColor(getResources().getColor(R.color.main));
                if (mTab04 == null)
                {
                    // 如果SettingFragment为空，则创建一个并添加到界面上
                    mTab04 = new PersonalFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    // 如果SettingFragment不为空，则直接将它显示出来
                    transaction.show(mTab04);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 清除掉所有的选中状态。
     */
    private void resetBtn()
    {
        ((ImageButton) mHome.findViewById(R.id.tabbar_btn_home))
                .setImageResource(R.mipmap.tabbar_home_normal);
        ((ImageButton) mLove.findViewById(R.id.tabbar_btn_love))
                .setImageResource(R.mipmap.tabbar_love_normal);
        ((ImageButton) mLibrary.findViewById(R.id.tabbar_btn_library))
                .setImageResource(R.mipmap.tabbar_reading_normal);
        ((ImageButton) mPersonal.findViewById(R.id.tabbar_btn_personal))
                .setImageResource(R.mipmap.tabbar_user_normal);
        mHomeText.setTextColor(getResources().getColor(R.color.default_black));
        mLoveText.setTextColor(getResources().getColor(R.color.default_black));
        mLibraryText.setTextColor(getResources().getColor(R.color.default_black));
        mPersonalText.setTextColor(getResources().getColor(R.color.default_black));
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction
     *            用于对Fragment执行操作的事务
     */
    @SuppressLint("NewApi")
    private void hideFragments(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }

    }


    public static void setLoveTextColor(Context context,int position){
        switch (position){
            case 0:
                mLoveText.setTextColor(context.getResources().getColor(R.color.main));
                ((ImageButton) mLove.findViewById(R.id.tabbar_btn_love))
                        .setImageResource(R.mipmap.tabbar_love_pressed);
                break;
            case 1:
                mLoveText.setTextColor(context.getResources().getColor(R.color.love));
                ((ImageButton) mLove.findViewById(R.id.tabbar_btn_love))
                        .setImageResource(R.mipmap.tabbar_love_pink);
                break;
            case 2:
                mLoveText.setTextColor(context.getResources().getColor(R.color.wish));
                ((ImageButton) mLove.findViewById(R.id.tabbar_btn_love))
                        .setImageResource(R.mipmap.tabbar_love_green);
                break;
        }

    }

    private void setFloatingMenu() {
        if(null!=mFloatingActionMenu){
            return;
        }
        //创建发布图标
        ImageView icon = new ImageView(this);
        icon.setImageDrawable(getResources().getDrawable(R.mipmap.lost_compose));

        //创建浮动发布按钮
        mFloatingActionButton = new FloatingActionButton.Builder(this).setContentView(icon).build();

        //创建约电影子浮动按钮
        SubActionButton.Builder itemBuilder = new SubActionButton.Builder(this);

        ImageView movieItem = new ImageView(this);
        movieItem.setImageDrawable(getResources().getDrawable(R.mipmap.movie_compose_menu));

        //创建表白墙子浮动按钮
        ImageView loveItem = new ImageView(this);
        loveItem.setImageDrawable(getResources().getDrawable(R.mipmap.love_compose_menu));

        //创建心愿墙子浮动按钮
        ImageView wishItem = new ImageView(this);
        wishItem.setImageDrawable(getResources().getDrawable(R.mipmap.wish_compose_menu
        ));

        mSubActionButtonMovie = itemBuilder.setContentView(movieItem).build();
        mSubActionButtonLove = itemBuilder.setContentView(loveItem).build();
        mSubActionButtonWish = itemBuilder.setContentView(wishItem).build();

        mSubActionButtonMovie.setLayoutParams(new AbsListView.LayoutParams(PicUtil.dip2px(this, 60f), PicUtil.dip2px(this, 60f)));
        mSubActionButtonLove.setLayoutParams(new AbsListView.LayoutParams(PicUtil.dip2px(this, 60f), PicUtil.dip2px(this, 60f)));
        mSubActionButtonWish.setLayoutParams(new AbsListView.LayoutParams(PicUtil.dip2px(this, 60f), PicUtil.dip2px(this, 60f)));

        mFloatingActionMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(mSubActionButtonMovie)
                .addSubActionView(mSubActionButtonLove)
                .addSubActionView(mSubActionButtonWish)
                .attachTo(mFloatingActionButton)
                .build();

        //约电影点击事件
        mSubActionButtonMovie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(MainActivity.this,ComposeLoveActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("flag", MOVIE_CODE);
                intent.putExtras(bundle);
                startActivityForResult(intent,MOVIE_CODE);
            }
        });
        //表白墙点击事件
        mSubActionButtonLove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,ComposeLoveActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("flag",LOVE_CODE);
                intent.putExtras(bundle);
                startActivityForResult(intent, LOVE_CODE);
            }
        });
        //心愿墙点击事件
        mSubActionButtonWish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,ComposeLoveActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("flag",WISH_CODE);
                intent.putExtras(bundle);
                startActivityForResult(intent, WISH_CODE);
            }
        });
        mFloatingActionButton.setY(-PicUtil.dip2px(this, 45));
        mFloatingActionButton.setX(PicUtil.dip2px(this, 10));
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
